package XynLeen.GraduationProject.Utils.Alarm;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import XynLeen.GraduationProject.Activities.Activity_Schedule;
import XynLeen.GraduationProject.R;

import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
import static android.content.Context.NOTIFICATION_SERVICE;
import static com.youth.xframe.XFrame.getSystemService;


public class AlarmReceiver extends BroadcastReceiver {


    protected NotificationManager mNotificationManager;
    private static final String PUSH_CHANNEL_ID = "PUSH_NOTIFY_ID";
    int pushNum=1;
    @Override
    public void onReceive(Context context, Intent intent) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelName = "订阅消息";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            createNotificationChannel(PUSH_CHANNEL_ID, channelName, importance);
        }

        int requestCode = intent.getIntExtra("requestCode",0);
        String title = intent.getStringExtra("title");
        if(mNotificationManager==null){
            mNotificationManager = (NotificationManager)
                    context.getSystemService(NOTIFICATION_SERVICE);
        }
        NotificationCompat.Builder builder;


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new NotificationCompat.Builder(context,PUSH_CHANNEL_ID);
        } else {
            builder = new NotificationCompat.Builder(context);
        }
        TipHelper.Vibrate(context,1000);
        Intent notificationIntent = new Intent(context, Activity_Schedule.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notificationIntent.putExtra("title",title);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, notificationIntent, FLAG_CANCEL_CURRENT);
        builder.setContentTitle(title)//设置通知栏标题
                .setContentIntent(pendingIntent) //设置通知栏点击意图
                .setContentText("事务提醒")
                .setNumber(++pushNum)
                .setTicker("通知内容") //通知首次出现在通知栏，带上升动画效果的
                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
                .setSmallIcon(R.mipmap.icon_clock)//设置通知小ICON
                .setChannelId(PUSH_CHANNEL_ID)
                .setDefaults( Notification.DEFAULT_ALL );

        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        mNotificationManager.notify(requestCode,notification);

    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

}